<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class category extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	
    function __construct() {
        parent::__construct();
        $this->load->model("muser");
        $this->load->model("scategory");
        $this->load->model("mcategory");
        $this->load->model("mresource");
		$this->load->model("msoft");
		
		$this->resCode = $this->mcategory->resCode;
        $this->url_index = admin_url() . "category";
        $this->folder_upload = "dataupload/category/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index() {
        $data = array();
        $datas = array();
        //= $this->mcategory->gets();
		$categoryParent = $this->mcategory->gets(array('CAT_PARENT'=>0));
		if ($categoryParent) {
			for($i=0; $i<count($categoryParent); $i++) {
				$categoryParent[$i]->CAT_NAME = "<strong>".$categoryParent[$i]->CAT_NAME."</strong>";
				$datas[] = $categoryParent[$i];
				
					$cate_child =  $this->scategory->get_cat_format_tree($categoryParent[$i]->CAT_ID);
					if ($cate_child) 
					foreach ($cate_child as $cc) {
						$cc['CAT_NAME'] = '|----- '.$cc['CAT_NAME'];
						$datas[] = (object)$cc;
						$cate_child2 =  $this->scategory->get_cat_format_tree($cc['CAT_ID']);
						if ($cate_child2) 
						foreach ($cate_child2 as $cc2) {
							$cc2['CAT_NAME'] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;------ '.$cc2['CAT_NAME'];
							$datas[] = (object)$cc2;
						}
					}
				
			}
		}
		$data['datas'] = $datas;
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $this->theme_data["main"] = "category/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($catID = 0) {
        $data = array();
        $post = $this->input->post();
        $data['cate'] = $this->mcategory->get($catID, 'CAT_ID');
		$data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
			if($upload = $this->upload(@$data['cate']->CAT_THUMB,@$data['cate']->CAT_CODE)){
				$post['cate']['CAT_THUMB'] = $this->folder_upload.$upload['upload_data']['file_name'];
			}
            if ($catID > 0) {
                $this->mcategory->update($catID, 'CAT_ID', $post['cate']);
            }
			else{
				$catID = $this->mcategory->insert($post['cate']);
			}
			$this->buid_category_router();
			$this->scategory->create_category_file_temp();
			$this->scategory->create_category_tree();
			$this->scategory->create_cat_format_tree();
			
			$this->msoft->update_cache_cate($catID);
            redirect($this->url_edit . $catID);
        }
		$cate_tree= array();
		$cate_tree[0] = "NONE";
		$categoryParent = $this->mcategory->gets(array('CAT_PARENT'=>0));
		if ($categoryParent) {
			for($i=0; $i<count($categoryParent); $i++) {
				$cate_tree[$categoryParent[$i]->CAT_ID] = $categoryParent[$i]->CAT_NAME;
				$cate_child =  $this->scategory->get_cat_format_tree($categoryParent[$i]->CAT_ID);
				if ($cate_child) 
				foreach ($cate_child as $cc) {
					$cate_tree[$cc['CAT_ID']] = "--- ".	$cc['CAT_NAME'];
				}
				
			}
		}
		$data['cate_tree'] = $cate_tree;
        $this->theme_data["main"] = "category/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($catID = 0){
		if($catID > 0){
			$info = $this->mcategory->get($catID, 'CAT_ID');
			if(file_exists('./'.$info->CAT_THUMB))
				unlink('./'.$info->CAT_THUMB);
			$this->mcategory->delete($catID, 'CAT_ID');
		}
	}
	
	function upload($old_name = '', $newName = '') {
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		if($newName && isset($newName) && $newName!='')
			$config['file_name']	= $newName.'-thumb';
			
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
				unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
	}
	function buid_category_router(){
		$cates = $this->mcategory->gets();
		$data_temp =array();
		$data = "<?php \n";
        $data.="return ";
		if($cates) {
			foreach($cates as $cate){
			if($cate->CAT_CODE != '')
				$data_temp[] = strtolower($cate->CAT_CODE);//array_push($data_temp,$cate->CAT_CODE);
			}
		$data_temp = var_export($data_temp, true);
		$data.= $data_temp;
        $data.="?>\n";
        write_file('./'.$this->scategory->folderCache.'category_router.php', $data);
		}
	}
}
